package saxion.speelveld.spaceengineer.database;

import java.util.ArrayList;

import saxion.speelveld.spaceengineer.model.Achievement;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class AchievementDAO
{
	private SQLiteDatabase database;

	private AchievementDB dbHelper;

	public AchievementDAO(Context context)
	{
		dbHelper = new AchievementDB(context);
	}

	/**
	 * Open the database connection.
	 * 
	 * @throws SQLException
	 */
	public void open() throws SQLException
	{
		database = dbHelper.getWritableDatabase();
	}

	/**
	 * Close the database connection.
	 */
	public void close()
	{
		dbHelper.close();
	}

	public ArrayList<Achievement> getAllAchievements()
	{
		ArrayList<Achievement> achievementList = new ArrayList<Achievement>();
		String sql = "SELECT * FROM " + AchievementDB.TABLE_NAME + ";";
		Cursor returnCursor = database.rawQuery(sql, null);
		returnCursor.moveToFirst();

		while (returnCursor.isAfterLast() == false)
		{
			achievementList.add(CursorToAchievement(returnCursor));
			returnCursor.moveToNext();
		}
		// returnCursor.close();

		return achievementList;
	}

	private Achievement CursorToAchievement(Cursor cursor)
	{
		Achievement achievement = new Achievement();
		achievement.setId(cursor.getInt(cursor.getColumnIndex(AchievementDB.COLUMN_ID)));
		achievement.setCaption(cursor.getString(cursor.getColumnIndex(AchievementDB.COLUMN_CAPTION)));
		achievement.setExplanation(cursor.getString(cursor.getColumnIndex(AchievementDB.COLUMN_EXPLANATION)));
		achievement.setAccomplished(intToBool(cursor.getInt(cursor.getColumnIndex(AchievementDB.COLUMN_ACCOMPLISHED))));
		return achievement;
	}

	private boolean intToBool(int input)
	{
		if (input == 1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	private int BooleanToInt(boolean input)
	{
		if (input == true)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}

	public void putDummyAchievements()
	{
		// Log.w("BLEGH", "putDummyAchievements!");
		ArrayList<Achievement> achievementList = new ArrayList<Achievement>();

		Achievement achievement = new Achievement();
		achievement.setCaption("Build a mine");
		achievement.setExplanation("You've got an achievement for building a mine.");
		achievement.setAccomplished(false);
		achievementList.add(achievement);

		achievement = new Achievement();
		achievement.setCaption("Complete game");
		achievement.setExplanation("Complete a game for the first time.");
		achievement.setAccomplished(false);
		achievementList.add(achievement);

		achievement = new Achievement();
		achievement.setCaption("Complete game 'insane.'");
		achievement.setExplanation("You've completed a game on difficulty 'insane.'");
		achievement.setAccomplished(false);
		achievementList.add(achievement);

		achievement = new Achievement();
		achievement.setCaption("Trade");
		achievement.setExplanation("You have bought 500 resources in the Space Market");
		achievement.setAccomplished(false);
		achievementList.add(achievement);

		achievement = new Achievement();
		achievement.setCaption("Happy");
		achievement.setExplanation("Your citizins are very happy.");
		achievement.setAccomplished(false);
		achievementList.add(achievement);

		achievement = new Achievement();
		achievement.setCaption("Miner");
		achievement.setExplanation("You have mined 10 sand");
		achievement.setAccomplished(false);
		achievementList.add(achievement);

		achievement = new Achievement();
		achievement.setCaption("Open the build screen");
		achievement.setExplanation("You've got an achievement for opening the build screen.");
		achievement.setAccomplished(false);
		achievementList.add(achievement);

		achievement = new Achievement();
		achievement.setCaption("Space Cube");
		achievement.setExplanation("You have builded 10 space cubes!");
		achievement.setAccomplished(false);
		achievementList.add(achievement);

		achievement = new Achievement();
		achievement.setCaption("Science");
		achievement.setExplanation("You've got an achievement for building a Space School and Research Lab.");
		achievement.setAccomplished(false);
		achievementList.add(achievement);

		for (Achievement achi : achievementList)
		{
			addAchievement(achi);
		}

	}

	public void addAchievement(Achievement achievement)
	{
		ContentValues values = new ContentValues();
		values.put(AchievementDB.COLUMN_CAPTION, achievement.getCaption());
		values.put(AchievementDB.COLUMN_EXPLANATION, achievement.getExplanation());
		values.put(AchievementDB.COLUMN_ACCOMPLISHED, BooleanToInt(achievement.isAccomplished()));
		database.insert(AchievementDB.TABLE_NAME, null, values);
	}

	public int accomplishedAchievement(int achievementId)
	{
		ContentValues values = new ContentValues();
		values.put(AchievementDB.COLUMN_ACCOMPLISHED, 1);
		return database.update(AchievementDB.TABLE_NAME, values, AchievementDB.COLUMN_ID + "=" + achievementId, null);
	}

	public Achievement getAchievement(int achievementId)
	{
		String sql = "SELECT * FROM " + AchievementDB.TABLE_NAME + " WHERE " + AchievementDB.COLUMN_ID + " = "
			+ achievementId + ";";
		Cursor cursor = database.rawQuery(sql, null);
		cursor.moveToFirst();

		return CursorToAchievement(cursor);
	}

	public boolean hasAchievements()
	{
		String sql = "SELECT COUNT(*) FROM " + AchievementDB.TABLE_NAME + ";";
		// SQLiteStatement statement = database.compileStatement(sql);
		// long count = statement.simpleQueryForLong();
		Cursor cursor = database.rawQuery(sql, null);
		cursor.moveToFirst();
		int rowCount = cursor.getInt(0);
		if (rowCount > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
